package com.stylefeng.guns.modular.vpn_app_version.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.VpnAppVersion;
import com.stylefeng.guns.modular.vpn_app_version.service.IVpnAppVersionService;

/**
 * 版本管理控制器
 *
 * @author fengshuonan
 * @Date 2020-03-14 09:15:00
 */
@Controller
@RequestMapping("/vpnAppVersion")
public class VpnAppVersionController extends BaseController {

    private String PREFIX = "/vpn_app_version/vpnAppVersion/";

    @Autowired
    private IVpnAppVersionService vpnAppVersionService;

    /**
     * 跳转到版本管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnAppVersion.html";
    }

    /**
     * 跳转到添加版本管理
     */
    @RequestMapping("/vpnAppVersion_add")
    public String vpnAppVersionAdd() {
        return PREFIX + "vpnAppVersion_add.html";
    }

    /**
     * 跳转到修改版本管理
     */
    @RequestMapping("/vpnAppVersion_update/{vpnAppVersionId}")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public String vpnAppVersionUpdate(@PathVariable Integer vpnAppVersionId, Model model) {
        VpnAppVersion vpnAppVersion = vpnAppVersionService.selectById(vpnAppVersionId);
        if(vpnAppVersion.getPlatform().equals("1")){
            vpnAppVersion.setPlatform("Android");
        }
        else if(vpnAppVersion.getPlatform().equals("2")){
            vpnAppVersion.setPlatform("IOS");
        }
        else if(vpnAppVersion.getPlatform().equals("3")){
            vpnAppVersion.setPlatform("Windows");
        }
        else if(vpnAppVersion.getPlatform().equals("4")){
            vpnAppVersion.setPlatform("Mac");
        }
        model.addAttribute("item",vpnAppVersion);
        LogObjectHolder.me().set(vpnAppVersion);
        return PREFIX + "vpnAppVersion_edit.html";
    }

    /**
     * 获取版本管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(String condition) {
        return vpnAppVersionService.selectList(null);
    }

    /**
     * 新增版本管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(VpnAppVersion vpnAppVersion) {
        vpnAppVersionService.addVersionInfo(vpnAppVersion);
        return SUCCESS_TIP;
    }

    /**
     * 删除版本管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object delete(@RequestParam Integer vpnAppVersionId) {
        vpnAppVersionService.deleteById(vpnAppVersionId);
        return SUCCESS_TIP;
    }

    /**
     * 修改版本管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object update(VpnAppVersion vpnAppVersion) {
        String platform = "1";
        if(vpnAppVersion.getPlatform().equals("Android")){
            platform = "1";
        }
        else if("IOS".equals(vpnAppVersion.getPlatform())){
            platform = "2";
        }
        else if("Windows".equals(vpnAppVersion.getPlatform())){
            platform = "3";
        }
        else if("Mac".equals(vpnAppVersion.getPlatform())){
            platform = "4";
        }
        vpnAppVersion.setPlatform(platform);
        vpnAppVersionService.updateById(vpnAppVersion);
        return SUCCESS_TIP;
    }
}
